package org.jeecg.modules.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.*;
import org.jeecg.modules.cw.mapper.CwAqhjwxjbtMapper;
import org.jeecg.modules.cw.mapper.CwAqhjwxjbtTjMapper;
import org.jeecg.modules.cw.service.ICwAqhjwxjbtTjService;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwJbszqkzbZhuService;
import org.jeecg.modules.cw.service.ICwZzrygzmxbService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 安全奖及未休奖补贴提交
 * @Author: jeecg-boot
 * @Date:   2025-09-02
 * @Version: V1.0
 */
@Service
public class CwAqhjwxjbtTjServiceImpl extends ServiceImpl<CwAqhjwxjbtTjMapper, CwAqhjwxjbtTj> implements ICwAqhjwxjbtTjService {

	@Autowired
	private CwAqhjwxjbtTjMapper cwAqhjwxjbtTjMapper;
	@Autowired
	private CwAqhjwxjbtMapper cwAqhjwxjbtMapper;
	@Autowired
	private ICwJbszqkzbZhuService cwJbszqkzbZhuService;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ICwZzrygzmxbService cwZzrygzmxbService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwAqhjwxjbtTj cwAqhjwxjbtTj, List<CwAqhjwxjbt> cwAqhjwxjbtList) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		cwAqhjwxjbtTjMapper.insert(cwAqhjwxjbtTj);
		if(cwAqhjwxjbtList!=null && cwAqhjwxjbtList.size()>0) {
			for(CwAqhjwxjbt entity:cwAqhjwxjbtList) {
				//外键设置
				entity.setMainId(cwAqhjwxjbtTj.getId());
				cwAqhjwxjbtMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwAqhjwxjbtTj cwAqhjwxjbtTj,List<CwAqhjwxjbt> cwAqhjwxjbtList) {
		cwAqhjwxjbtTjMapper.updateById(cwAqhjwxjbtTj);
		//1.先删除子表数据
		cwAqhjwxjbtMapper.deleteByMainId(cwAqhjwxjbtTj.getId());
		//安全财政
		Double anquancz=0.0;
		Double anquanfcz=0.0;
		Double weixiucz=0.0;
		Double weixiufcz=0.0;
		//2.子表数据重新插入
		if(cwAqhjwxjbtList!=null && cwAqhjwxjbtList.size()>0) {
			for(CwAqhjwxjbt entity:cwAqhjwxjbtList) {
				if ("安全".equals(entity.getAqWx())){
					if ("财政".equals(entity.getCzFcz())){
						if (entity.getMoney()==null){
							entity.setMoney(0.0);
						}
						anquancz+=entity.getMoney();
					}else {
						if (entity.getMoney()==null){
							entity.setMoney(0.0);
						}
						anquanfcz+=entity.getMoney();
					}
				}else {
					if ("财政".equals(entity.getCzFcz())){
						if (entity.getMoney()==null){
							entity.setMoney(0.0);
						}
						weixiucz+=entity.getMoney();
					}else {
						if (entity.getMoney()==null){
							entity.setMoney(0.0);
						}
						weixiufcz+=entity.getMoney();
					}
				}
				//外键设置
				entity.setMainId(cwAqhjwxjbtTj.getId());
				cwAqhjwxjbtMapper.insert(entity);
			}
		}
//		LambdaQueryWrapper<CwZzrygzmxb> wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(CwZzrygzmxb::getYear,cwAqhjwxjbtTj.getYear())
//				.eq(CwZzrygzmxb::getUnit,cwAqhjwxjbtTj.getUnit());
//		Double njxgzcz=0.0;
//		Double njxgzfcz=0.0;
//		Double jtbtcz=0.0;
//		Double jtbtfcz=0.0;
//		for (CwZzrygzmxb cwZzrygzmxb : cwZzrygzmxbService.list(wrapper)) {
//			if ("财政".equals(cwZzrygzmxb.getCzFcz())){
//				jtbtcz+=cwZzrygzmxb.getNjtbt();
//				njxgzcz+=cwZzrygzmxb.getNjxgz();
//			}else {
//				njxgzfcz+=cwZzrygzmxb.getNjxgz();
//				jtbtfcz+=cwZzrygzmxb.getNjtbt();
//			}
//		}


//		LambdaQueryWrapper<CwJbszqkzb> cwJbszqkzbLambdaQueryWrapper = new LambdaQueryWrapper<>();
//		cwJbszqkzbLambdaQueryWrapper.eq(CwJbszqkzb::getYear,cwAqhjwxjbtTj.getYear())
//				.eq(CwJbszqkzb::getUnit,cwAqhjwxjbtTj.getUnit()).in(CwJbszqkzb::getProject,"绩效工资","津贴补贴");
//		for (CwJbszqkzb cwJbszqkzb : cwJbszqkzbService.list(cwJbszqkzbLambdaQueryWrapper)) {
//			if ("绩效工资".equals(cwJbszqkzb.getProject())){
//				cwJbszqkzb.setUnit(cwAqhjwxjbtTj.getUnit());
//				cwJbszqkzb.setYear(cwAqhjwxjbtTj.getYear());
//				cwJbszqkzb.setFinancial(njxgzcz+anquancz);
//				cwJbszqkzb.setNonFinancials(njxgzfcz+anquanfcz);
//				if (cwJbszqkzb.getYsshj()==null){
//					cwJbszqkzb.setYsshj(0.0);
//				}
//				cwJbszqkzb.setYsshj(njxgzcz+njxgzfcz+anquancz+anquanfcz);
//				cwJbszqkzb.setCwPx(16);
//				cwJbszqkzbService.updateById(cwJbszqkzb);
//			}
//			if ("津贴补贴".equals(cwJbszqkzb.getProject())){
//				cwJbszqkzb.setUnit(cwAqhjwxjbtTj.getUnit());
//				cwJbszqkzb.setYear(cwAqhjwxjbtTj.getYear());
//				cwJbszqkzb.setFinancial(jtbtcz+weixiucz);
//				cwJbszqkzb.setNonFinancials(jtbtfcz+weixiufcz);
//				if (cwJbszqkzb.getYsshj()==null){
//					cwJbszqkzb.setYsshj(0.0);
//				}
//				cwJbszqkzb.setYsshj(jtbtcz+weixiucz+jtbtfcz+weixiufcz);
//				cwJbszqkzb.setCwPx(13);
//				cwJbszqkzbService.updateById(cwJbszqkzb);
//			}
//		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwAqhjwxjbtMapper.deleteByMainId(id);
		cwAqhjwxjbtTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwAqhjwxjbtMapper.deleteByMainId(id.toString());
			cwAqhjwxjbtTjMapper.deleteById(id);
		}
	}
	
}
